package com.mpyy.handler.everyday;

import com.alibaba.fastjson.JSON;
import com.mpyy.pojo.*;
import com.mpyy.service.everyday.SalesmanServicInter;
import com.mpyy.tool.Tool;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @PackageName:com.mpyy.handler.everyday   业务员管理
 * @ClassName:Salesmanhandler
 * @Date:2022-03-11:8:00
 * @Author: Tang Neng
 */
@Controller
public class SalesmanHandler {
    @Autowired
    private SalesmanServicInter salesmanServicInter;


    /**
     * 员工信息搜索
     * @param empId
     * @return
     */
    @RequestMapping("/searchSalesmanInfo")
    @ResponseBody
    public String searchSalesmanInfo(String empId){
        List<EmployeeInfo> employeeInfos = salesmanServicInter.searchSalesmanInfo(empId);
        //装换成JSON格式的字符串，返回到页面
        return Tool.hashMapInfo(employeeInfos,employeeInfos);
    }


    /**
     * 员工销售采购情况
     * @param empId
     * @return
     */
    @RequestMapping("/searchPurchaseMarketInfo")
    @ResponseBody
    public String searchPurchaseMarketInfo(String empId){
        List<SaleOrder> saleOrders = salesmanServicInter.searchSaleOrderInfo(empId);
        //装换成JSON格式的字符串，返回到页面
        return Tool.hashMapInfo(saleOrders , saleOrders);
    }
    /**
     * 员工销售采购情况明细
     * @param string
     * @return
     */
    @RequestMapping("/searchPurchaseMarketInfoDes")
    @ResponseBody
    public String searchPurchaseMarketInfoDes(String string ){
        List<SaleOrderDesc> saleOrderDescs = salesmanServicInter.searchSaleOrderDescInfo(string);
        //装换成JSON格式的字符串，返回到页面
        return Tool.hashMapInfo(saleOrderDescs , saleOrderDescs);
    }
    @Getter
    @Setter
    @ToString
    @AllArgsConstructor
    class PurchaseMarketInfo{
        private String documentNumber;//单据号
        private String client;//供货商客户
        private String warehouseaName;//仓库名称
        private Double amountPayable;//应付金额
        private Double amountPaid;//实付金额
        private Double purchaseAmount;//采购金额
        private Double wePaid;//我方已付
    }
    /**
     * 采购销售表统一数据
     * @param empId
     * @return
     */
    @RequestMapping("/searchPurchaseMarketUnifyInfo")
    @ResponseBody
    public String searchPurchaseMarketUnifyInfo(String empId){
        List<PurchaseMarketInfo> purchaseMarketInfos = new ArrayList<>();//实例化一个保存销售采购信息的集合用于存储信息
        List<SaleOrder> saleOrders = salesmanServicInter.searchSaleOrderInfo(empId);//员工销售信息
        List<SaleReturn> saleReturns = salesmanServicInter.searchSaleReturnInfo(empId);//员工销售退货信息
        List<PurchaseStock> purchaseStocks = salesmanServicInter.searchPurchaseStockInfo(empId);//员工采购信息
        List<PurchaseReturn> purchaseReturns = salesmanServicInter.searchPurchaseReturnInfo(empId);//员工采购退货
        if (null != purchaseReturns && purchaseReturns.size() != 0){
            for(PurchaseReturn purchaseReturn : purchaseReturns){
                purchaseMarketInfos.add(new PurchaseMarketInfo(purchaseReturn.getPurRetId(), purchaseReturn.getSupplierInfo().getSupName(), purchaseReturn.getDepotInfo().getDepotName(), 0.0,0.0, Double.parseDouble("-"+purchaseReturn.getPurRetRefund()), Double.parseDouble("-"+purchaseReturn.getPurRetRealRefund())));
            }
        }
        if (null != purchaseStocks && purchaseStocks.size() != 0){//判断采购表是否纯在数据
            for(PurchaseStock purchaseStock : purchaseStocks){
                purchaseMarketInfos.add(new PurchaseMarketInfo(purchaseStock.getPurStoId(),purchaseStock.getSupplierInfo().getSupName(),purchaseStock.getDepotInfo().getDepotName(),0.0,0.0,purchaseStock.getPurStoPayable(),purchaseStock.getPurStoOutof())
                );
            }
        }
        if(null != saleOrders && saleOrders.size() != 0){//判断员工销售信息是否存在数据
            for (SaleOrder saleOrder : saleOrders){//遍历当前集合
                // 给集合添加数据
                purchaseMarketInfos.add(new PurchaseMarketInfo(saleOrder.getSalOrdId(),saleOrder.getCustomerInfo().getCustName(),saleOrder.getDepotInfo().getDepotName(), saleOrder.getSalOrdReceivable(), saleOrder.getSalOrdPaidIn(),0.0,0.0)
                );
            }
        }
        if(null != saleReturns && saleReturns.size() != 0){//判断员工销售退货信息是否存在数据
            for (SaleReturn saleReturn : saleReturns){//遍历当前集合
                // 给集合添加数据
                purchaseMarketInfos.add(
                        new PurchaseMarketInfo(saleReturn.getSalRetId(),saleReturn.getCustomerInfo().getCustName(),saleReturn.getDepotInfo().getDepotName(),Double.parseDouble("-"+saleReturn.getSalRetRealRefund()),Double.parseDouble("-"+saleReturn.getSalRetRefunded()),0.0,0.0)
                );
            }
        }
        //装换成JSON格式的字符串，返回到页面
        return Tool.hashMapInfo(purchaseMarketInfos,purchaseMarketInfos);

    }
    @Getter
    @Setter
    @ToString
    @AllArgsConstructor
    class PurchaseMarketInfoDescs{
        private String proId;
        private String proName;
        private String unitId;
        private Double proBid;
        private int pronum;
    }
    /**
     * 判断订单是那个类型然后进行该订单详情查询
     * @param string
     * @return
     */
    @RequestMapping(value = "/searchJudgeOrderInfo" , produces = "appliction/json;charset=UTF-8")
    @ResponseBody
    public String searchJudgeOrderInfo(String string){

        List<PurchaseMarketInfoDescs> purchaseMarketInfoArr = new ArrayList<>();

        if("XS".equals(string.substring(0,2))){//判断是否是销售订单
            List<SaleOrderDesc> saleOrderDescs = salesmanServicInter.searchSaleOrderDescInfo(string);
            for (SaleOrderDesc saleOrderDesc : saleOrderDescs){//遍历当前集合
                // 给集合添加数据
                purchaseMarketInfoArr.add(
                        new PurchaseMarketInfoDescs(saleOrderDesc.getProductId(),saleOrderDesc.getSalOrdDescProName(),saleOrderDesc.getSalOrdDescUnit(),saleOrderDesc.getSalOrdDescProPrice(),saleOrderDesc.getSalOrdDescProNum())
                );
            }
        }else if ("XT".equals(string.substring(0,2))){//判断是否是销售退货订单
            List<SaleReturnDesc> saleReturnDescs = salesmanServicInter.searchSaleReturnDesc(string);
            for (SaleReturnDesc saleReturnDesc : saleReturnDescs){//遍历当前集合
                // 给集合添加数据
                purchaseMarketInfoArr.add(
                        new PurchaseMarketInfoDescs(saleReturnDesc.getProductId(),saleReturnDesc.getSalRetDescProName(),saleReturnDesc.getSalRetDescUnit(),saleReturnDesc.getSalRetDescProPrice(),saleReturnDesc.getSalRetDescNum())
                );
            }
        }else if ("CG".equals(string.substring(0,2))){//判断是否是采购订单
            List<PurchaseStockDesc> purchaseStockDescs = salesmanServicInter.searchPurchaseStockDescInfo(string);
            for (PurchaseStockDesc purchaseStockDesc : purchaseStockDescs){//遍历当前集合
                // 给集合添加数据
                purchaseMarketInfoArr.add(
                        new PurchaseMarketInfoDescs(purchaseStockDesc.getProId(),purchaseStockDesc.getProName(),purchaseStockDesc.getPurStoDescUnit(),purchaseStockDesc.getPurStoDescPrice(),purchaseStockDesc.getPurStoDescNum())
                );
            }
        }else if ("CT".equals(string.substring(0,2))){//判断是否是采购退货订单
            List<PurchaseReturnDesc> purchaseReturnDescs = salesmanServicInter.searchPurchaseReturnDescInfo(string);
            for (PurchaseReturnDesc purchaseReturnDesc : purchaseReturnDescs){//遍历当前集合
                // 给集合添加数据
                purchaseMarketInfoArr.add(
                        new PurchaseMarketInfoDescs(purchaseReturnDesc.getProId(),purchaseReturnDesc.getProName(),purchaseReturnDesc.getPurRetDescUnit(),purchaseReturnDesc.getPurRetDescPrice(),purchaseReturnDesc.getPurRetDescNum())
                );
            }
        }
        //装换成JSON格式的字符串，返回到页面
        return Tool.hashMapInfo(purchaseMarketInfoArr , purchaseMarketInfoArr);
    }
}
